import got from "got";
import { readFileByType } from "./util";

const prefixUrl = process.env.ICAN_PREFIXURL;
const postBuyUrl = process.env.ICAN_BUY_PRODUCT;
const token = readFileByType('access_token')
const addressList = readFileByType('address', 'json')

class OrderDTO {
  constructor(id: number) {
    this.contactId = id
  }
  amount = 1
  contactId = 51
  id = 286
  source = "1"
  type = 1
}

const buyProduct = async () => {
  const headers = {
    authorization: `Bearer ${token}`
  }
  const postUrl = `${prefixUrl}${postBuyUrl}`;
  if (addressList[0]) {
    const orderDto = new OrderDTO(addressList[0].id)
    try {
      const res = await got.post(postUrl, {
        method: "POST",
        headers,
        json: orderDto
      });
      console.log('购买成功!');
      return res;
    } catch (error) {
      console.error(error);
    }

  }
}

export { buyProduct }